/*
* @(#)DocTypeFilterTag.java
*
* Copyright (c) 2004 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;

/**
  <p>Class Name:    DocTypeFilterTag.java    </p>
  <p>Description:   This tag is the layout the document type filter.

  <p>Modification in "dms.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;javax.servlet.jsp.tagxt.BodyTagSupport&lt;/tagclass&gt;
     [TO]
     &lt;tagclass&gt;com.dcivision.dms.taglib.layout.LocationPathTag&lt;/tagclass&gt;

     [ADDED]
     &lt;attribute&gt;
     &lt;name&gt;parentID&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;rootID&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;permission&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;rootDocument&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
  </pre>
  </p>

 * @author           Jenny Li
 * @company          DCIVision Limited
 * @creation date    17/05/2005
 * @version          $Revision: 1.8.2.3 $
 */

public class DocTypeFilterTag extends BodyTagSupport{
  
  public static final String REVISION = "$Revision: 1.8.2.3 $";
  
  private static final Log log = LogFactory.getLog(DocTypeFilterTag.class);
  
  /**
   * Method called at start of tag
   * @return a SKIP_BODY
   */
  public int doStartTag() throws JspException {
    return SKIP_BODY;
  }

  public int doEndTag() throws JspException{
    JspWriter out = pageContext.getOut();
    String displayDocType=com.dcivision.dms.web.ListDmsDocumentForm.DEFAULT_DISPLAY_DOC_TYPE;
    String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    boolean hasPaperDocRight=permissionManager.hasAccessRight(SystemFunctionConstant.DMS_PAPER_DOCUMENT,"R");
    boolean hasCompoundDocRight=permissionManager.hasAccessRight(SystemFunctionConstant.DMS_COMPOUND_DOCUMENT,"R");
    boolean enablePaperDoc=SystemParameterFactory.getSystemParameterBoolean(SystemParameterConstant.DMS_ENABLE_PAPER_DOCUMENT);
    Integer createUser=(Integer)this.pageContext.getSession().getAttribute("createUser");
    if(sessionContainer.getUserRecordID().equals(createUser)){
      if(!Utility.isEmpty(this.pageContext.getSession().getAttribute(DmsOperationConstant.DISPLAY_DOC_TYPE))){
        displayDocType=(String)this.pageContext.getSession().getAttribute(DmsOperationConstant.DISPLAY_DOC_TYPE);
      }
    }
    StringBuffer docInfo=new StringBuffer();
    
    docInfo.append("<style>");
    docInfo.append(".volume {background- color: white; border-top:solid #F5F5F5 1px; border-left:solid #F5F5F5 1px; border-right:solid #E9E9E9 1px; border-bottom:solid #E9E9E9 1px;} ");
    docInfo.append("</style>");
    docInfo.append("<table class='volume' align='left'> ");
    docInfo.append("<tr><td align='left'> ");
    docInfo.append(" <a href=");
    docInfo.append("\"#\" onClick=\"javascript:return(PopupSelectDocType('"+displayDocType+"'));\">");
    if(displayDocType.indexOf(DmsDocument.DOCUMENT_TYPE)>=0){
      docInfo.append("<img ");
      docInfo.append("src=\"" + imgSrc("IconDMSFileExtUNKNOWN.gif") + "\" ");
      docInfo.append("align=\"absmiddle\" ");
      docInfo.append("border=\"0\" ");
      docInfo.append("/>");
    }else{
      docInfo.append("<img ");
      docInfo.append("src=\"" + imgSrc("IconDMSFileExtUNKNOWNDisable.gif") + "\" ");
      docInfo.append("align=\"absmiddle\" ");
      docInfo.append("border=\"0\" ");
      docInfo.append("/>");
    }
    if(displayDocType.indexOf(DmsDocument.FOLDER_TYPE)>=0){
      docInfo.append("<img ");
      docInfo.append("src=\"" + imgSrc("IconDMSFolder.gif") + "\" ");
      docInfo.append("align=\"absmiddle\" ");
      docInfo.append("border=\"0\" ");
      docInfo.append("/>");
        }else{
      docInfo.append("<img ");
      docInfo.append("src=\"" + imgSrc("IconDMSFolderDisable.gif") + "\" ");
      docInfo.append("align=\"absmiddle\" ");
      docInfo.append("border=\"0\" ");
      docInfo.append("/>");
    }
    if(displayDocType.indexOf(DmsDocument.DOCUMENT_LINK)>=0){
      docInfo.append("<img ");
      docInfo.append("src=\"" + imgSrc("IconDMSFileExtUNKNOWNShortcut.gif") + "\" ");
      docInfo.append("align=\"absmiddle\" ");
      docInfo.append("border=\"0\" ");
      docInfo.append("/>");
    }else{
      docInfo.append("<img ");
      docInfo.append("src=\"" + imgSrc("IconDMSFileExtUNKNOWNShortCutDisable.gif") + "\" ");
      docInfo.append("align=\"absmiddle\" ");
      docInfo.append("border=\"0\" ");
      docInfo.append("/>");
    }
    if(hasCompoundDocRight){
      if(displayDocType.indexOf(DmsDocument.COMPOUND_DOC_TYPE)>=0){
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconDMSFileExtCOMPOUND.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }else{
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconDMSFileExtCOMPOUNDDisable.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }
    }
    if(hasPaperDocRight && enablePaperDoc){
      if(displayDocType.indexOf(DmsDocument.PAPER_DOC_TYPE)>=0){
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconDMSPaperDoc.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }else{
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconDMSPaperDocDisable.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }         
    }
    if(displayDocType.indexOf(DmsDocument.EMAIL_DOC_TYPE)>=0){
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconTreeEmailDocOpen.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }else{
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconTreeEmailDocClose.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
    }
    /*joan.xiong update code for bug EIP-1167 by 2006/12/11
    if(displayDocType.indexOf(DmsDocument.FORM_DOC_TYPE)>=0){
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconTreeFormDocOpen.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }else{
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconTreeFormDocClose.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }
    if(displayDocType.indexOf(DmsDocument.FLOW_DOC_TYPE)>=0){
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconTreeFlowDocOpen.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }else{
        docInfo.append("<img ");
        docInfo.append("src=\"" + imgSrc("IconTreeFlowDocClose.gif") + "\" ");
        docInfo.append("align=\"absmiddle\" ");
        docInfo.append("border=\"0\" ");
        docInfo.append("/>");
      }
      joan.xiong update code end*/
    docInfo.append("</a>");
    docInfo.append("</td></tr></table>");
    
    try{
      out.print(docInfo.toString());
    }catch (java.io.IOException ioe) {
      log.error(ioe, ioe);
      throw new JspException(ioe);
    }
    return EVAL_PAGE;
  }
  
  protected String imgSrc(String icon) throws javax.servlet.jsp.JspException {
    String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    String preference = SystemParameterFactory.getSystemParameter(SystemParameterConstant.PREFERENCE);
    if ((sessionContainer!=null) && (sessionContainer.getUserRecord()!=null)) {
      if (sessionContainer.getUserRecord().getPreference()!=null) {
        preference = sessionContainer.getUserRecord().getPreference().toString();
      }
    }
    return (contextPath + "/theme/pref" + preference + "/img/common/" + icon);
  }
  
}